package fyp.spedmew;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.parse.ParseObject;

public class UpdateLocationActivity extends Activity implements LocationListener {
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    // The minimum time between updates in milliseconds
    //  private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    private static final long MIN_TIME_BW_UPDATES = 1000 * 1 * 1; // 1 sec
    protected LocationManager locationManager;
    protected Context context;
    protected boolean gps_enabled, network_enabled;
    private TextView txtLat;
    private LocationManager mLocManager;
    private FusedLocationService fusedLocationService;



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        fusedLocationService = new FusedLocationService(this);
        setContentView(R.layout.activity_updatelocation);

        txtLat = (TextView) findViewById(R.id.textview1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
        gps_enabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
        network_enabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (gps_enabled) {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        } else if (network_enabled) {
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        };

    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
                + location.getLongitude());

        ParseObject P_Location = new ParseObject("Location");


        P_Location.put("JobID", "a");
        P_Location.put("Latitude",location.getLatitude() );
        P_Location.put("Longitude",location.getLongitude() );
        P_Location.saveInBackground();

    }

    public void btnclick(View view) {
      //  Location location = fusedLocationService.getLocation();

        mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        String provider = mLocManager.getBestProvider(criteria, true);

        Location location = mLocManager.getLastKnownLocation(provider);


        String locationResult = "";
        double latitude =  location.getLatitude();
        double longitude = location.getLongitude();

        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
                + location.getLongitude());

        ParseObject P_Location = new ParseObject("Location");


        P_Location.put("JobID", "sj");
        P_Location.put("Latitude",location.getLatitude() );
        P_Location.put("Longitude",location.getLongitude() );
        P_Location.saveInBackground();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude", "disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude", "enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude", "status");
    }
}